//
//  WFWhatEatViewController.m
//  WeiFang
//
//  Created by WeiFang on 2022.
//  Copyright © 2022 WeiFang. All rights reserved.
//

#import "WFWhatEatViewController.h"

#import "WFCustomizeCandidateMenuViewController.h"

@interface WFWhatEatViewController ()

@property (strong,nonatomic) UIScrollView *scrollView;
@property (strong,nonatomic) UIButton *startButton;
@property (strong,nonatomic) UIButton *customButton;
@property (assign,nonatomic) NSInteger states;//0.开始 1.停止 2.再来一次
@property (nonatomic,strong) NSTimer *timer;
@property (assign,nonatomic) NSInteger index;
@property (copy,nonatomic) NSArray *eatList;

@end

@implementation WFWhatEatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationItemTitleViewWithTitle:@"一会吃什么"];
    [self setscrollNumer];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (NSArray *)eatList {
    NSArray *list = [kDataManager getEatArray];
    NSMutableArray *a = [NSMutableArray arrayWithCapacity:list.count];
    [list enumerateObjectsUsingBlock:^(WFEatModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        [a addObject:model.food];
    }];
    NSArray *ary = a;
    if (_states == 0) {
        ary = @[@"今天吃什么"];
    }
    return ary;
}

- (void)setscrollNumer {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth/2 - 348/2, self.navigationBarHeight + 80, 348, 204)];
    view.backgroundColor = [UIColor clearColor];
 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.bounds];
    imageView.image = [UIImage imageNamed:@"eat_bg"];
    [view addSubview:imageView];
    
    [self.view addSubview:view];
    
    UIView *scrollFatherView = [[UIView alloc] initWithFrame:CGRectMake(view.frame.size.width/2 - 238/2,40,238, 60)];
    scrollFatherView.backgroundColor = [UIColor clearColor];
    scrollFatherView.clipsToBounds = YES;
    [view addSubview:scrollFatherView];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollFatherView.frame.size.width/2 - 238/2,15,238, 30)];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.clipsToBounds = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [scrollFatherView addSubview:self.scrollView];
    
    [self setupSubViews];
    
    self.startButton.frame = CGRectMake(view.frame.size.width/2 - 163/2, view.frame.size.height - 51 - 10, 163, 51);
    [view addSubview:self.startButton];
    
    self.customButton.frame = CGRectMake(kScreenWidth/2 - 258/2, view.frame.origin.y + view.frame.size.height + 60, 258, 52);
    [self.view addSubview:self.customButton];
}

- (void)setupSubViews {
    // 移除之前的子控件
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSInteger pageCount = self.eatList.count;
    for (NSInteger i = 0 ; i < pageCount; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, i*self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
                        
        NSString *str = self.eatList[i];
        
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.text = [NSString stringWithFormat:@"%@",str];
        label.font = [UIFont boldSystemFontOfSize:20];
        label.textColor = UIColorFromHex(0x821113);
        label.textAlignment = NSTextAlignmentCenter;
        [view addSubview:label];

        [self.scrollView addSubview:view];
    }
    self.scrollView.contentSize = CGSizeMake(0, self.scrollView.frame.size.height * pageCount);
}

- (void)myButtonAction:(UIButton *)sender {
    if (_index >= self.eatList.count - 1) {
        _index = 0;
    } else {
        _index ++;
    }
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x,self.scrollView.frame.size.height * _index) animated:NO];
}

#pragma mark - Action

- (void)buttonAction:(UIButton *)sender {
    if (_states == 0) {
        //开始
        NSArray *list = [kDataManager getEatArray];
        if (list.count < 2) {
            [self showProgressHUD:@"请至少添加2项自定义候选菜单"];
            return;
        }
        _states = 1;
        [self.startButton setImage:[UIImage imageNamed:@"stop_bt_bg"] forState:UIControlStateNormal];
        [self setupSubViews];
        self.scrollView.clipsToBounds = NO;
        [self endWaitTimeKeeping];
        self->_timer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                        target:self
                                                      selector:@selector(daoJiShi)
                                                      userInfo:nil
                                                       repeats:YES];
    } else if (_states == 1) {
        //停止
        _states = 2;
        [self.startButton setImage:[UIImage imageNamed:@"again_bt_bg"] forState:UIControlStateNormal];
        [self endWaitTimeKeeping];
        self.scrollView.clipsToBounds = YES;
    } else if (_states == 2) {
        //再来一次
        NSArray *list = [kDataManager getEatArray];
        if (list.count < 2) {
            [self showProgressHUD:@"请至少添加2项自定义候选菜单"];
            return;
        }
        _states = 1;
        [self.startButton setImage:[UIImage imageNamed:@"stop_bt_bg"] forState:UIControlStateNormal];
        [self setupSubViews];
        self.scrollView.clipsToBounds = NO;
        [self endWaitTimeKeeping];
        self->_timer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                        target:self
                                                      selector:@selector(daoJiShi)
                                                      userInfo:nil
                                                       repeats:YES];
    }
}

- (void)backAction:(UIButton *)sender {
    [self endWaitTimeKeeping];
    [super backAction:sender];
}

#pragma mark - goto

- (void)gotoCustomizeCandidateMenuViewController {
    WFCustomizeCandidateMenuViewController* vc = [WFCustomizeCandidateMenuViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 倒计时

/**
 *  倒计时
 */
- (void)daoJiShi {
    [self myButtonAction:nil];
}

/**
 *  结束计时
 */
- (void)endWaitTimeKeeping {
    [_timer invalidate];
    _timer = nil;
}

#pragma mark - 懒加载

- (UIButton *)startButton {
    if (!_startButton) {
        _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startButton setImage:[UIImage imageNamed:@"start_bt_bg"] forState:UIControlStateNormal];
        [_startButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startButton;
}

- (UIButton *)customButton {
    if (!_customButton) {
        _customButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _customButton.backgroundColor = UIColorFromHex(0x000000);
        _customButton.layer.cornerRadius = 6;
        _customButton.layer.masksToBounds = YES;
        [_customButton setTitle:@"自定义候选菜单" forState:UIControlStateNormal];
        [_customButton setTitleColor:UIColorFromHex(0xFFFFFF) forState:UIControlStateNormal];
        _customButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [_customButton addTarget:self action:@selector(gotoCustomizeCandidateMenuViewController) forControlEvents:UIControlEventTouchUpInside];
    }
    return _customButton;
}

@end



